package com.hy.mydesktop.client.component.factory;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import com.extjs.gxt.ui.client.Registry;
import com.extjs.gxt.ui.client.Style.HorizontalAlignment;
import com.extjs.gxt.ui.client.data.BaseModel;
import com.extjs.gxt.ui.client.data.ModelData;
import com.extjs.gxt.ui.client.event.ButtonEvent;
import com.extjs.gxt.ui.client.event.SelectionListener;
import com.extjs.gxt.ui.client.mvc.AppEvent;
import com.extjs.gxt.ui.client.store.ListStore;
import com.extjs.gxt.ui.client.store.Record;
import com.extjs.gxt.ui.client.widget.Component;
import com.extjs.gxt.ui.client.widget.Container;
import com.extjs.gxt.ui.client.widget.Layout;
import com.extjs.gxt.ui.client.widget.MessageBox;
import com.extjs.gxt.ui.client.widget.button.Button;
import com.extjs.gxt.ui.client.widget.form.CheckBox;
import com.extjs.gxt.ui.client.widget.form.CheckBoxGroup;
import com.extjs.gxt.ui.client.widget.form.ComboBox;
import com.extjs.gxt.ui.client.widget.form.Field;
import com.extjs.gxt.ui.client.widget.form.FormButtonBinding;
import com.extjs.gxt.ui.client.widget.form.FormPanel;
import com.extjs.gxt.ui.client.widget.form.RadioGroup;
import com.extjs.gxt.ui.client.widget.form.FormPanel.Method;
import com.extjs.gxt.ui.client.widget.grid.Grid;
import com.extjs.gxt.ui.client.widget.layout.FormLayout;
import com.google.gwt.core.client.GWT;
import com.google.gwt.user.client.Window;
import com.google.gwt.user.client.rpc.AsyncCallback;
import com.google.gwt.user.client.ui.AbstractImagePrototype;
import com.hy.mydesktop.client.MyDesktop;
import com.hy.mydesktop.client.component.event.AppEvents;
import com.hy.mydesktop.client.component.meta.AccordionPanelModelEnum;
import com.hy.mydesktop.client.component.meta.FormPanelModelEnum;

import com.hy.mydesktop.client.component.meta.WindowModelEnum;
import com.hy.mydesktop.client.component.type.ComponentTypeEnum;
import com.hy.mydesktop.client.rpc.ServiceInvoker;
import com.hy.mydesktop.client.rpc.ServiceInvokerAsync;
import com.hy.mydesktop.shared.persistence.domain.gxt.GxtComponentMetaNodeModel;
import com.hy.mydesktop.shared.rpc.meta.GxtToSeamServiceParameter;
import com.hy.mydesktop.shared.rpc.meta.GxtToSeamServiceResult;
import com.hy.mydesktop.shared.rpc.util.BaseModelUtil;
import com.hy.mydesktop.shared.rpc.util.ComponentTypeEnumConverter;

/**
 * 
 * 
 * <ul>
 * <li>开发作者：花宏宇</li>
 * <li>设计日期：2012-2-12；时间：上午11:05:30</li>
 * <li>类型名称：FormpanelFactory</li>
 * <li>设计目的：</li>
 * </ul>
 * <ul>
 * <b>修订编号：</b>
 * <li>修订日期：</li>
 * <li>修订作者：</li>
 * <li>修订原因：</li>
 * <li>修订内容：</li>
 * </ul>
 */
public class FormpanelFactory2012_2_19 {
	
	private static FormButtonBinding binding;
		
	public static FormPanel createFormPanel(
			GxtComponentMetaNodeModel gxtComponentMetaNodeModel) {
		/*for (String propertyName : gxtComponentMetaNodeModel.getPropertyNames()) {
			System.err.println(propertyName+"  "+gxtComponentMetaNodeModel.get(propertyName));
		}*/
		return configureFormPanel(gxtComponentMetaNodeModel);

	}

	/**
	 * 
	 * 
	 * @param gxtComponentMetaNodeModel
	 * @return
	 */
	private static FormPanel configureFormPanel(
			GxtComponentMetaNodeModel gxtComponentMetaNodeModel) {

		 FormPanel panel = createDefaultFormPanel();
		 panel = AbstractComponetFactory.configureAbstractComponet(panel, gxtComponentMetaNodeModel);
		 panel = AbstractLayoutContainerComponetFactory.configureAbstractLayoutContainerComponet(panel, gxtComponentMetaNodeModel);
		 panel= (FormPanel)ContentPanelFactory.configureAttributesOfContentPanel(panel, gxtComponentMetaNodeModel);

		// panel.setFieldWidth(250);
		// panel.setBodyBorder(false);
		 panel.setBorders(false);
		 panel.setInsetBorder(false);		 
		 panel.setPadding(0);
		
		
		//添加到父容器时，设置LayoutData
		if (gxtComponentMetaNodeModel.get(FormPanelModelEnum.LAYOUTDATAINPARENTCONTAINER.name()
				.toLowerCase()) != null) {
			String layoutdatainparentcontainer = (String) gxtComponentMetaNodeModel
					.get(FormPanelModelEnum.LAYOUTDATAINPARENTCONTAINER.name().toLowerCase());
			panel.setData("layoutdatainparentcontainer", layoutdatainparentcontainer);

		}
		if (gxtComponentMetaNodeModel.get(FormPanelModelEnum.ACTION.name()
				.toLowerCase()) != null) {
			String url = (String) gxtComponentMetaNodeModel
					.get(FormPanelModelEnum.ACTION.name().toLowerCase());
			panel.setAction(url);

		} 
		//设置按钮对齐方式
		if (gxtComponentMetaNodeModel.get(FormPanelModelEnum.BUTTONALIGN
				.name().toLowerCase()) != null) {
			String buttonAlin = ((String) gxtComponentMetaNodeModel
					.get(FormPanelModelEnum.BUTTONALIGN.name().toLowerCase()))
					.toUpperCase();
			if (buttonAlin.equals("CENTER")) {
				panel.setButtonAlign(HorizontalAlignment.CENTER);
			}else if (buttonAlin.equals("LEFT")) {
				panel.setButtonAlign(HorizontalAlignment.LEFT);
			}
		}
		//设置是否折叠
		if (gxtComponentMetaNodeModel.get(FormPanelModelEnum.COLLAPSIBLE
				.name().toLowerCase()) != null) {
			Boolean collapsible = (Boolean) gxtComponentMetaNodeModel
					.get(FormPanelModelEnum.COLLAPSIBLE.name().toLowerCase());
			panel.setCollapsible((boolean) collapsible);

		} 				
		//设置标题
		
		//设置布局
		
		//设置布局
		
		//设置提交方式
		if (gxtComponentMetaNodeModel.get(FormPanelModelEnum.METHOD.name()
				.toLowerCase()) != null) {
			String method = ((String) gxtComponentMetaNodeModel
					.get(FormPanelModelEnum.HEADING.name().toLowerCase()))
					.toUpperCase();
			if(method.equals("POST")){
				panel.setMethod(Method.POST);
			}else{
				panel.setMethod(Method.GET);
			}

		} 
		//设置图标
		
		//设置添加按钮
		
		//设置添加提交按钮
		if(gxtComponentMetaNodeModel.get(FormPanelModelEnum.ADDSUBMITBUTTON.name()
				.toLowerCase()) != null){
//			Boolean addSubmitButton=(Boolean)gxtComponentMetaNodeModel.get(FormPanelModelEnum.ADDSUBMITBUTTON.name()
//					.toLowerCase());
			
			//for(int i=0;i<list.size();i++)
			//{
				//Button button=ButtonFactory.createButton(list.get(i));
			 	String buttonName = (String)(gxtComponentMetaNodeModel.get(FormPanelModelEnum.SUBMITBUTTON.name()
						.toLowerCase()));
			 	if (buttonName == null) {
					buttonName = "提交";
				}
				Button button= new Button(buttonName);
				button = registerSubmitFormEvent(panel, button, gxtComponentMetaNodeModel);
				if(true)
				{
					FormButtonBinding binding=new FormButtonBinding(panel);
					 binding.addButton(button);
				}else
				{
					button.addSelectionListener(new SelectionListener<ButtonEvent>() {
						
						@Override
						public void componentSelected(ButtonEvent ce) {
							//换成MessageBox
							Map<String, Object> box=new HashMap<String, Object>();
							box.put("component_model_type", ComponentTypeEnum.MESSAGEBOX_MODEL);
							box.put("type", "confirm");
							box.put("title", "温馨提示");
							box.put("message", "您确定要取消吗？");
							GxtComponentMetaNodeModel box1=new GxtComponentMetaNodeModel(box);
							MessageBoxFactory.createMessageBox(box1);
						}
					});
				}
				panel.addButton(button);
			//}			
		}
		//设置添加重置按钮
		if(gxtComponentMetaNodeModel.get(FormPanelModelEnum.ADDRESETBUTTON.name()
				.toLowerCase()) != null){
//			Boolean addResetButton=(Boolean)gxtComponentMetaNodeModel.get(FormPanelModelEnum.ADDRESETBUTTON.name()
//					.toLowerCase());
			//for(int i=0;i<list.size();i++)
			//{
				//Button button=ButtonFactory.createButton(list.get(i));
			 	String buttonName = (String)(gxtComponentMetaNodeModel.get(FormPanelModelEnum.RESETBUTTON.name()
						.toLowerCase()));
			 	if (buttonName == null) {
					buttonName = "重置";
				}
				Button button= new Button(buttonName);
				
				button = registerResetFormEvent(panel, button);
				
				panel.addButton(button);
			//}			
		}
		
		
		/*if(gxtComponentMetaNodeModel.get(FormPanelModelEnum.ADDBUTTON.name()
				.toLowerCase()) != null){
			List<GxtComponentMetaNodeModel> list=(List<GxtComponentMetaNodeModel>)gxtComponentMetaNodeModel.get(FormPanelModelEnum.ADDBUTTON.name()
					.toLowerCase());
			for(int i=0;i<list.size();i++)
			{
				Button button=ButtonFactory.createButton(list.get(i));
				if(i==0)
				{
					FormButtonBinding binding=new FormButtonBinding(formPanel);
					 binding.addButton(button);
				}else
				{
					button.addSelectionListener(new SelectionListener<ButtonEvent>() {
						
						@Override
						public void componentSelected(ButtonEvent ce) {
							//换成MessageBox
							Map<String, Object> box=new HashMap<String, Object>();
							box.put("component_model_type", ComponentTypeEnum.MESSAGEBOX_MODEL);
							box.put("type", "confirm");
							box.put("title", "温馨提示");
							box.put("message", "您确定要取消吗？");
							GxtComponentMetaNodeModel box1=new GxtComponentMetaNodeModel(box);
							MessageBoxFactory.createMessageBox(box1);
						}
					});
				}
				formPanel.addButton(button);
			}			
		}*/
		
		//panel.addButton(new Button("FormpanelFactory"));
		//panel.setButtonAlign(HorizontalAlignment.CENTER);   

		return panel;
	}

	private static Button registerResetFormEvent(final FormPanel formPanel,
			Button button) {
		
		button.addSelectionListener(new SelectionListener<ButtonEvent>() {
			
			@Override
			public void componentSelected(ButtonEvent ce) {
				
				 
				List<Grid<?>> grids = new ArrayList<Grid<?>>();
				locateGrids(formPanel,grids);
				for (Grid<?> grid : grids) {
					
					ListStore<ModelData> listStore = ((Grid<ModelData>)grid).getStore();
					//ListStore<ModelData> listStore = grid.getStore();
					listStore.rejectChanges();
				}
				
				List<Field<?>> fields = formPanel.getFields();
				for (Field<?> field : fields) {
					if (field.isDirty()) {
						System.out.println(field.getName()+" is dirty");						
					}
					field.reset();
				}
				
			
			}
		});
		return button;
	}

	private static Button registerSubmitFormEvent(final FormPanel formPanel,Button button,GxtComponentMetaNodeModel gxtComponentMetaNodeModel){
		final ServiceInvokerAsync serviceInvoker = Registry.get(MyDesktop.SERVICEINVOKER);
		/**
		 * 为表单的提交按钮注册提交事件
		 * 由于FormPanel控件，默认支持表达的提交，
		 * 所以，暂时，不通过Gwt的Mvc，转发AppEvents.FORM_BUTTON_SUBMINT_ONCLICK事件，
		 * 实现表单的提交
		 */
		
		/**
		 * 表单提交的数据及格式
		 * 数据：
		 * 1、包含窗口控制器的id或者窗口控件的id
		 * 2、表单中的Controller的id或Component的id，为key，
		 * 3、表格的的Controller的id或Component的id，为key，
		 * 格式：
		 * 暂定，不传输控件对应的组件名称和组件方法（用于提交的：增、改）
		 */
		button.addSelectionListener(new SelectionListener<ButtonEvent>() {

			@Override
			public void componentSelected(ButtonEvent ce) {
				//Window.alert("getServiceComponentName before");
			
//				final GxtToSeamServiceParameter gxtToSeamServiceParameter = formPanel.getData("gxtToSeamServiceParameterForMerge");
				final GxtToSeamServiceParameter gxtToSeamServiceParameter = formPanel.getData("gxtToSeamServiceParameter");
				Window.alert("getServiceComponentName 000"+gxtToSeamServiceParameter.getServiceComponentName());
				BaseModel[] methodArgumentsForSubmit = new BaseModel[1];
				methodArgumentsForSubmit[0] = new BaseModel();
				/*if (formPanel.getData("formDataForSubmit")!=null) {
					if (formPanel.getData("formDataForSubmit") instanceof BaseModel) {
//						methodArgumentsForSubmit[0] = (BaseModel)formPanel.getData("formDataForSubmit");
						methodArgumentsForSubmit[0] = new BaseModel();

						BaseModelUtil.encapsulateComponentData(methodArgumentsForSubmit[0], formPanel.getData("id")+".identifier", ((BaseModel)formPanel.getData("formDataForSubmit")).get("identifier"));

						//methodArgumentsForSubmit[0].set(formPanel.getData("id")+".identifier", ((BaseModel)formPanel.getData("formDataForSubmit")).get("identifier"));
						Window.alert(formPanel.getData("id")+".identifier :"+((BaseModel)formPanel.getData("formDataForSubmit")).get("identifier").toString());

					} else {
						methodArgumentsForSubmit[0] = new BaseModel();
						Window.alert("formPanel.getData(formDataForSubmit)  is  not null ");
					}
					//设置为空
					formPanel.setData("formDataForSubmit",null);
				} else {
					methodArgumentsForSubmit[0] = new BaseModel();
					Window.alert("formPanel.getData(formDataForSubmit)  is  null ");
				}*/
				

				//对表单中的默认数据进行赋值
				if (formPanel.getData("formDefaultData")!=null&&formPanel.getData("formDefaultData") instanceof BaseModel) {
					Window.alert("formPanel.getData(formDefaultData) :"+((BaseModel)formPanel.getData("formDefaultData")).toString());

					for (String property : ((BaseModel)formPanel.getData("formDefaultData")).getPropertyNames()) {
						BaseModelUtil.encapsulateComponentData(methodArgumentsForSubmit[0], property, ((BaseModel)formPanel.getData("formDefaultData")).get(property));
						Window.alert(property+" :"+((BaseModel)formPanel.getData("formDefaultData")).toString());
					}
				}
				
				gxtToSeamServiceParameter.setMethodArguments(methodArgumentsForSubmit );
				
						 //提取表格中的脏数据，用于提交
						final List<Grid<?>> grids = new ArrayList<Grid<?>>();
						locateGrids(formPanel,grids);
						for (Grid<?> grid : grids) {
							System.err.println("*******************************");
							//System.err.println(grid.getClass());
							ListStore<ModelData> listStore = ((Grid<ModelData>)grid).getStore();
							List<Record> records = listStore.getModifiedRecords();
							List<ModelData> modelDatas = new ArrayList<ModelData>();
							for (Record record : records) {
								ModelData modelData = record.getModel();
								modelDatas.add(modelData);
								for (String prop : modelData.getPropertyNames()) {
									System.err.println(prop+" : "+modelData.get(prop));
									//Window.alert(prop+" : "+modelData.get(prop));
					    	  }
								//String nameString = modelData.get("name");
								//modelData.get("name");
							}
							if (modelDatas.size()>0) {
								
								BaseModelUtil.encapsulateComponentData(methodArgumentsForSubmit[0], (String)grid.getData("id"), modelDatas);
								//methodArgumentsForSubmit[0].set((String)grid.getData("id"), modelDatas);
								//GxtToSeamServiceParameter gxtToSeamServiceParameterForMerge = grid.getData("gxtToSeamServiceParameterForAdd");
								/*GxtToSeamServiceParameter gxtToSeamServiceParameterForAdd = grid.getData("gxtToSeamServiceParameterForAdd");
								GxtToSeamServiceParameter gxtToSeamServiceParameterForUpdate = grid.getData("gxtToSeamServiceParameterForUpdate");
								;
								methodArgumentsForSubmit[0].set("ServiceName_"+BaseModelUtil.getFirstPrefix((String)grid.getData("id")), gxtToSeamServiceParameterForAdd.getServiceComponentName());
								System.err.println("ServiceName_"+BaseModelUtil.getFirstPrefix((String)grid.getData("id"))+ gxtToSeamServiceParameterForAdd.getServiceComponentName());
								//Window.alert("ServiceName_"+BaseModelUtil.getFirstPrefix((String)grid.getData("id"))+ gxtToSeamServiceParameterForAdd.getServiceComponentName());
								methodArgumentsForSubmit[0].set("MethodNameForAdd_"+BaseModelUtil.getFirstPrefix((String)grid.getData("id")), gxtToSeamServiceParameterForAdd.getServiceMethodName());
								System.err.println("MethodNameForAdd_"+(String)grid.getData("id")+ gxtToSeamServiceParameterForAdd.getServiceMethodName());;
								//Window.alert("MethodNameForAdd_"+(String)grid.getData("id")+ gxtToSeamServiceParameterForAdd.getServiceMethodName());;
								methodArgumentsForSubmit[0].set("MethodNameForUpdate_"+BaseModelUtil.getFirstPrefix((String)grid.getData("id")), gxtToSeamServiceParameterForUpdate.getServiceMethodName());
								System.err.println("MethodNameForUpdate_"+(String)grid.getData("id")+ gxtToSeamServiceParameterForUpdate.getServiceMethodName());
								//Window.alert("MethodNameForUpdate_"+(String)grid.getData("id")+ gxtToSeamServiceParameterForUpdate.getServiceMethodName());
								 */
							}
						}
						
						methodArgumentsForSubmit[0]=getMethodeArgumentsOfForm(formPanel,methodArgumentsForSubmit[0]);
						
				Window.alert("getServiceComponentName "+gxtToSeamServiceParameter.getServiceComponentName());
				if (formPanel.getData("formDataForSubmit")!=null) {
					if (formPanel.getData("formDataForSubmit") instanceof BaseModel) {
//						methodArgumentsForSubmit[0] = (BaseModel)formPanel.getData("formDataForSubmit");
						
						//methodArgumentsForSubmit[0] = new BaseModel();

						BaseModelUtil.encapsulateComponentData(methodArgumentsForSubmit[0], formPanel.getData("id")+".identifier", ((BaseModel)formPanel.getData("formDataForSubmit")).get("identifier"));

						//methodArgumentsForSubmit[0].set(formPanel.getData("id")+".identifier", ((BaseModel)formPanel.getData("formDataForSubmit")).get("identifier"));
						Window.alert("formPanel.getData(formDataForSubmit)  is  not null and "+formPanel.getData("id")+".identifier :"+((BaseModel)formPanel.getData("formDataForSubmit")).get("identifier").toString());
						Window.alert("formPanel.getData(formDataForSubmit)  is  not null and "+formPanel.getData("id")+".identifier :"+methodArgumentsForSubmit[0].get(formPanel.getData("id")+".identifier"));

					} else {
						//methodArgumentsForSubmit[0] = new BaseModel();
						Window.alert("formPanel.getData(formDataForSubmit)  is  not null ");

					}
					//设置为空
					formPanel.setData("formDataForSubmit",null);
				} else {
					//methodArgumentsForSubmit[0] = new BaseModel();
					Window.alert("formPanel.getData(formDataForSubmit)  is  null ");


				}

				BaseModel pojo = new BaseModel();
				pojo = methodArgumentsForSubmit[0].get(formPanel.getData("id").toString());
				for (String property : pojo.getPropertyNames()) {
					
					Window.alert(formPanel.getData("id").toString() +"."+ property+" :"+pojo.get(property));

				}

				 MessageBox.info(formPanel.getData("id")+".identifier", methodArgumentsForSubmit[0].get(formPanel.getData("id")+".identifier").toString(), null); 
				 
				serviceInvoker.callService(gxtToSeamServiceParameter , new AsyncCallback<GxtToSeamServiceResult>() {
					
					@Override
					public void onSuccess(GxtToSeamServiceResult result) {
						for (Grid grid : grids) {
							
							
							
							//grid.getStore().commitChanges();
							grid.getStore().getLoader().load();
						}
						//Window.alert("提交成功");
						 MessageBox.info("Information", "提交成功！", null); 
						//TODO grid.getStore().commitChanges();
					}
					
					@Override
					public void onFailure(Throwable caught) {
						 MessageBox.info("Information", "提交失败！", null); 
					}
				});
			}

			
		
			
		});
		return button;
	}
	
	
	private static Button registerSubmitFormEvent2(final FormPanel formPanel,Button button,GxtComponentMetaNodeModel gxtComponentMetaNodeModel){
		final ServiceInvokerAsync serviceInvoker = Registry.get(MyDesktop.SERVICEINVOKER);
		/**
		 * 为表单的提交按钮注册提交事件
		 * 由于FormPanel控件，默认支持表达的提交，
		 * 所以，暂时，不通过Gwt的Mvc，转发AppEvents.FORM_BUTTON_SUBMINT_ONCLICK事件，
		 * 实现表单的提交
		 */
		
		/**
		 * 表单提交的数据及格式
		 * 数据：
		 * 1、包含窗口控制器的id或者窗口控件的id
		 * 2、表单中的Controller的id或Component的id，为key，
		 * 3、表格的的Controller的id或Component的id，为key，
		 * 格式：
		 * 暂定，不传输控件对应的组件名称和组件方法（用于提交的：增、改）
		 */
		button.addSelectionListener(new SelectionListener<ButtonEvent>() {

			@Override
			public void componentSelected(ButtonEvent ce) {			
//				final GxtToSeamServiceParameter gxtToSeamServiceParameter = formPanel.getData("gxtToSeamServiceParameterForMerge");
				final GxtToSeamServiceParameter gxtToSeamServiceParameter = formPanel.getData("gxtToSeamServiceParameter");

				BaseModel[] methodArgumentsForSubmit = new BaseModel[1];
				methodArgumentsForSubmit[0] = new BaseModel();

				//对表单中的默认数据进行赋值
				if (formPanel.getData("formDefaultData")!=null&&formPanel.getData("formDefaultData") instanceof BaseModel) {
					Window.alert("formPanel.getData(formDefaultData) :"+((BaseModel)formPanel.getData("formDefaultData")).toString());

					for (String property : ((BaseModel)formPanel.getData("formDefaultData")).getPropertyNames()) {
						BaseModelUtil.encapsulateComponentData(methodArgumentsForSubmit[0], property, ((BaseModel)formPanel.getData("formDefaultData")).get(property));
						Window.alert(property+" :"+((BaseModel)formPanel.getData("formDefaultData")).toString());

					}
				}
				
				gxtToSeamServiceParameter.setMethodArguments(methodArgumentsForSubmit );
						 //提取表格中的脏数据，用于提交
						final List<Grid<?>> grids = new ArrayList<Grid<?>>();
						locateGrids(formPanel,grids);
						for (Grid<?> grid : grids) {
							System.err.println("*******************************");
							//System.err.println(grid.getClass());
							ListStore<ModelData> listStore = ((Grid<ModelData>)grid).getStore();
							List<Record> records = listStore.getModifiedRecords();
							List<ModelData> modelDatas = new ArrayList<ModelData>();
							for (Record record : records) {
								ModelData modelData = record.getModel();
								modelDatas.add(modelData);
								for (String prop : modelData.getPropertyNames()) {
									System.err.println(prop+" : "+modelData.get(prop));
									//Window.alert(prop+" : "+modelData.get(prop));
					    	  }
								//String nameString = modelData.get("name");
								//modelData.get("name");
							}
							if (modelDatas.size()>0) {
								//??????????????????????????????????
								BaseModelUtil.encapsulateComponentData(methodArgumentsForSubmit[0], (String)grid.getData("id"), modelDatas);
								//methodArgumentsForSubmit[0].set((String)grid.getData("id"), modelDatas);
								//GxtToSeamServiceParameter gxtToSeamServiceParameterForMerge = grid.getData("gxtToSeamServiceParameterForAdd");
							}
						}
						MessageBox.info("Information", "收集脏数据！", null); 
						//////////////////////////////////////////////////////////////////////////////////////
						///methodArgumentsForSubmit[0]=getMethodeArgumentsOfForm(formPanel,methodArgumentsForSubmit[0]);
						//////////////////////////////////////////////////////////////////////////////
				Window.alert("getServiceComponentName "+gxtToSeamServiceParameter.getServiceComponentName());
				if (formPanel.getData("formDataForSubmit")!=null) {
					if (formPanel.getData("formDataForSubmit") instanceof BaseModel) {
//						methodArgumentsForSubmit[0] = (BaseModel)formPanel.getData("formDataForSubmit");
						//methodArgumentsForSubmit[0] = new BaseModel();
						BaseModelUtil.encapsulateComponentData(methodArgumentsForSubmit[0], formPanel.getData("id")+".identifier", ((BaseModel)formPanel.getData("formDataForSubmit")).get("identifier"));

						//methodArgumentsForSubmit[0].set(formPanel.getData("id")+".identifier", ((BaseModel)formPanel.getData("formDataForSubmit")).get("identifier"));
						Window.alert("formPanel.getData(formDataForSubmit)  is  not null and "+formPanel.getData("id")+".identifier :"+((BaseModel)formPanel.getData("formDataForSubmit")).get("identifier").toString());
						Window.alert("formPanel.getData(formDataForSubmit)  is  not null and "+formPanel.getData("id")+".identifier :"+methodArgumentsForSubmit[0].get(formPanel.getData("id")+".identifier"));

					} else {
						//methodArgumentsForSubmit[0] = new BaseModel();
						Window.alert("formPanel.getData(formDataForSubmit)  is  not null ");

					}
					//设置为空
					formPanel.setData("formDataForSubmit",null);
				} else {
					//methodArgumentsForSubmit[0] = new BaseModel();
					Window.alert("formPanel.getData(formDataForSubmit)  is  null ");
				}

				BaseModel pojo = new BaseModel();
				pojo = methodArgumentsForSubmit[0].get(formPanel.getData("id").toString());
				for (String property : pojo.getPropertyNames()) {
					Window.alert(formPanel.getData("id").toString() +"."+ property+" :"+pojo.get(property));
				}

				 MessageBox.info(formPanel.getData("id")+".identifier", methodArgumentsForSubmit[0].get(formPanel.getData("id")+".identifier").toString(), null); 
				 
				serviceInvoker.callService(gxtToSeamServiceParameter , new AsyncCallback<GxtToSeamServiceResult>() {
					
					@Override
					public void onSuccess(GxtToSeamServiceResult result) {
						//提交表格的脏数据之后，重新加载表格数据
						for (Grid grid : grids) {
							//grid.getStore().commitChanges();
							grid.getStore().getLoader().load();
						}
						//Window.alert("提交成功");
						 MessageBox.info("Information", "提交成功！", null); 
						//TODO grid.getStore().commitChanges();
					}
					
					@Override
					public void onFailure(Throwable caught) {
						 MessageBox.info("Information", "提交失败！", null); 
					}
				});
			}

			
		});
		return button;
	}
	
	
	/*private void locateGrid(Container<Component> gridSetModule,List<Grid> grids) {
		System.err.println(gridSetModule.getClass());
		for (Component component : gridSetModule.getItems()) {
			System.err.println(component.getClass());
			if (component instanceof Grid) {
				if (grid ==null) {
					grid = (Grid)component;
				}
				
				
			}else if(component instanceof Container){
				locateGrid((Container)component,grid);
			}
		}
		
	}*/
	
	
	private static BaseModel getMethodeArgumentsOfForm(FormPanel formPanel,BaseModel baseModel) {
		BaseModel[] methodArgumentsForSubmit= new BaseModel[1];
		methodArgumentsForSubmit[0]=baseModel;
		//遍历formPanel的所有Field类型的子元素
		String id = null;
		List<Field<?>> fields = formPanel.getFields();
		for (Field<?> field : formPanel.getFields()) {
			System.err.println("*******************************");
			System.err.println(field.getClass());
			//提取RadioGroup类型的控件中的数据，并封装到，作为服务方法中的参数，提交
			if (field instanceof RadioGroup) {
				String name = ((RadioGroup)field).getName();
				String name2 = ((RadioGroup)field).getFieldLabel();
				String value = ((RadioGroup)field).getValue().getName();
				//((RadioGroup)field).getv.getValue().getName();
				String value2 = ((RadioGroup)field).getValue().getBoxLabel();
				//System.err.println(name+"----------"+value);
				System.err.println(name2+"----------"+value2);
//				methodArgumentsForSubmit[0].set(name2, value2);
				//methodArgumentsForSubmit[0].set(field.getId(), value2);
				methodArgumentsForSubmit[0].set((String)field.getData("id"), value2);
				
				GxtToSeamServiceParameter gxtToSeamServiceParameterForAdd = field.getData("gxtToSeamServiceParameterForAdd");
				GxtToSeamServiceParameter gxtToSeamServiceParameterForUpdate = field.getData("gxtToSeamServiceParameterForUpdate");
				
				if (gxtToSeamServiceParameterForAdd!=null&&gxtToSeamServiceParameterForUpdate!=null) {
					
					methodArgumentsForSubmit[0].set("ServiceName_"+BaseModelUtil.getFirstPrefix((String)field.getData("id")), gxtToSeamServiceParameterForAdd.getServiceComponentName());
					System.err.println("ServiceName_"+BaseModelUtil.getFirstPrefix((String)field.getData("id"))+"  "+ gxtToSeamServiceParameterForAdd.getServiceComponentName());
					//Window.alert("ServiceName_"+BaseModelUtil.getFirstPrefix((String)field.getData("id"))+"  "+ gxtToSeamServiceParameterForAdd.getServiceComponentName());
					methodArgumentsForSubmit[0].set("MethodNameForAdd_"+BaseModelUtil.getFirstPrefix((String)field.getData("id")), gxtToSeamServiceParameterForAdd.getServiceMethodName());
					System.err.println("MethodNameForAdd_"+BaseModelUtil.getFirstPrefix((String)field.getData("id"))+"  "+ gxtToSeamServiceParameterForAdd.getServiceMethodName());;
					//Window.alert("MethodNameForAdd_"+BaseModelUtil.getFirstPrefix((String)field.getData("id"))+"  "+ gxtToSeamServiceParameterForAdd.getServiceMethodName());;
					methodArgumentsForSubmit[0].set("MethodNameForUpdate_"+BaseModelUtil.getFirstPrefix((String)field.getData("id")), gxtToSeamServiceParameterForUpdate.getServiceMethodName());
					System.err.println("MethodNameForUpdate_"+BaseModelUtil.getFirstPrefix((String)field.getData("id"))+"  "+ gxtToSeamServiceParameterForUpdate.getServiceMethodName());
					//Window.alert("MethodNameForUpdate_"+BaseModelUtil.getFirstPrefix((String)field.getData("id"))+"  "+ gxtToSeamServiceParameterForUpdate.getServiceMethodName());
					
					/*methodArgumentsForSubmit[0].set("ServiceName_"+(String)field.getData("id"), gxtToSeamServiceParameterForAdd.getServiceComponentName());
					System.err.println("ServiceName_"+(String)field.getData("id")+"  "+ gxtToSeamServiceParameterForAdd.getServiceComponentName());
					Window.alert("ServiceName_"+(String)field.getData("id")+"  "+ gxtToSeamServiceParameterForAdd.getServiceComponentName());
					methodArgumentsForSubmit[0].set("MethodNameForAdd_"+(String)field.getData("id"), gxtToSeamServiceParameterForAdd.getServiceMethodName());
					System.err.println("MethodNameForAdd_"+(String)field.getData("id")+"  "+ gxtToSeamServiceParameterForAdd.getServiceMethodName());;
					Window.alert("MethodNameForAdd_"+(String)field.getData("id")+"  "+ gxtToSeamServiceParameterForAdd.getServiceMethodName());;
					methodArgumentsForSubmit[0].set("MethodNameForUpdate_"+(String)field.getData("id"), gxtToSeamServiceParameterForUpdate.getServiceMethodName());
					System.err.println("MethodNameForUpdate_"+(String)field.getData("id")+"  "+ gxtToSeamServiceParameterForUpdate.getServiceMethodName());
					Window.alert("MethodNameForUpdate_"+(String)field.getData("id")+"  "+ gxtToSeamServiceParameterForUpdate.getServiceMethodName());
				*/
				
				}
				
				
			}
			//提取CheckBoxGroup类型的控件中的数据，并封装到，作为服务方法中的参数，提交
			else if (field instanceof CheckBoxGroup) {
				String name = ((CheckBoxGroup)field).getName();
				String name2 = ((CheckBoxGroup)field).getFieldLabel();
				List<CheckBox> checkBoxs = ((CheckBoxGroup)field).getValues();
				/*List<String> values= new ArrayList<String>();
				for (CheckBox checkBox : checkBoxs) {
					values.add(checkBox.getName());
				}
				//System.err.println(name+"----------"+values);
				System.err.println(name2+"----------"+values);
//				methodArgumentsForSubmit[0].set(name2, values);
				methodArgumentsForSubmit[0].set(field.getName(), values);*/
				for (CheckBox checkBox : checkBoxs) {
					//methodArgumentsForSubmit[0].set((String)checkBox.getData("id"), checkBox.getValue());
					BaseModelUtil.encapsulateComponentData(methodArgumentsForSubmit[0], (String)checkBox.getData("id"), checkBox.getValue());

					
					
					GxtToSeamServiceParameter gxtToSeamServiceParameterForAdd = field.getData("gxtToSeamServiceParameterForAdd");
					GxtToSeamServiceParameter gxtToSeamServiceParameterForUpdate = field.getData("gxtToSeamServiceParameterForUpdate");

					if (gxtToSeamServiceParameterForAdd!=null&&gxtToSeamServiceParameterForUpdate!=null) {
						
						methodArgumentsForSubmit[0].set("ServiceName_"+BaseModelUtil.getFirstPrefix((String)field.getData("id")), gxtToSeamServiceParameterForAdd.getServiceComponentName());
						System.err.println("ServiceName_"+BaseModelUtil.getFirstPrefix((String)field.getData("id"))+"  "+ gxtToSeamServiceParameterForAdd.getServiceComponentName());
						//Window.alert("ServiceName_"+BaseModelUtil.getFirstPrefix((String)field.getData("id"))+"  "+ gxtToSeamServiceParameterForAdd.getServiceComponentName());
						methodArgumentsForSubmit[0].set("MethodNameForAdd_"+BaseModelUtil.getFirstPrefix((String)field.getData("id")), gxtToSeamServiceParameterForAdd.getServiceMethodName());
						System.err.println("MethodNameForAdd_"+BaseModelUtil.getFirstPrefix((String)field.getData("id"))+"  "+ gxtToSeamServiceParameterForAdd.getServiceMethodName());;
						//Window.alert("MethodNameForAdd_"+BaseModelUtil.getFirstPrefix((String)field.getData("id"))+"  "+ gxtToSeamServiceParameterForAdd.getServiceMethodName());;
						methodArgumentsForSubmit[0].set("MethodNameForUpdate_"+BaseModelUtil.getFirstPrefix((String)field.getData("id")), gxtToSeamServiceParameterForUpdate.getServiceMethodName());
						System.err.println("MethodNameForUpdate_"+BaseModelUtil.getFirstPrefix((String)field.getData("id"))+"  "+ gxtToSeamServiceParameterForUpdate.getServiceMethodName());
						//Window.alert("MethodNameForUpdate_"+BaseModelUtil.getFirstPrefix((String)field.getData("id"))+"  "+ gxtToSeamServiceParameterForUpdate.getServiceMethodName());
						/*methodArgumentsForSubmit[0].set("ServiceName_"+(String)field.getData("id"), gxtToSeamServiceParameterForAdd.getServiceComponentName());
						System.err.println("ServiceName_"+(String)field.getData("id")+"  "+ gxtToSeamServiceParameterForAdd.getServiceComponentName());
						Window.alert("ServiceName_"+(String)field.getData("id")+"  "+ gxtToSeamServiceParameterForAdd.getServiceComponentName());
						methodArgumentsForSubmit[0].set("MethodNameForAdd_"+(String)field.getData("id"), gxtToSeamServiceParameterForAdd.getServiceMethodName());
						System.err.println("MethodNameForAdd_"+(String)field.getData("id")+"  "+ gxtToSeamServiceParameterForAdd.getServiceMethodName());;
						Window.alert("MethodNameForAdd_"+(String)field.getData("id")+"  "+ gxtToSeamServiceParameterForAdd.getServiceMethodName());;
						methodArgumentsForSubmit[0].set("MethodNameForUpdate_"+(String)field.getData("id"), gxtToSeamServiceParameterForUpdate.getServiceMethodName());
						System.err.println("MethodNameForUpdate_"+(String)field.getData("id")+"  "+ gxtToSeamServiceParameterForUpdate.getServiceMethodName());
						Window.alert("MethodNameForUpdate_"+(String)field.getData("id")+"  "+ gxtToSeamServiceParameterForUpdate.getServiceMethodName());*/
						
						
					}
														
				}
				


			}
			//提取ComboBox类型的控件中的数据，并封装到，作为服务方法中的参数，提交
			else if (field instanceof ComboBox) {
				String name = ((ComboBox)field).getName();
				//String name2 = ((CheckBoxGroup)field).getFieldLabel();
				ModelData modelData = ((ComboBox)field).getValue();
				//String value= modelData.get("name");
				//System.err.println(name+"----------"+value);
				String seamComponetName = (String)field.getData("seamComponetName");

				//System.err.println(name+"----------"+values);
				
//				methodArgumentsForSubmit[0].set(name, value);
//				methodArgumentsForSubmit[0].set(field.getId(), value);
				//methodArgumentsForSubmit[0].set((String)field.getData("id"), value);
				id = (String)field.getData("id");
				BaseModelUtil.encapsulateComponentData(methodArgumentsForSubmit[0], id, field.getValue());
				BaseModelUtil.encapsulateComponentData(methodArgumentsForSubmit[0], 
						BaseModelUtil.getPrefix(id)+".seamComponetName", seamComponetName);
				
				//Window.alert("comboBox id :"+id);

				GxtToSeamServiceParameter gxtToSeamServiceParameterForAdd = field.getData("gxtToSeamServiceParameterForAdd");
				GxtToSeamServiceParameter gxtToSeamServiceParameterForUpdate = field.getData("gxtToSeamServiceParameterForUpdate");
				;
				/*BaseModelUtil.getFirstPrefix((String)field.getData("id"));
				methodArgumentsForSubmit[0].set("ServiceName_"+BaseModelUtil.getFirstPrefix((String)field.getData("id")), gxtToSeamServiceParameterForAdd.getServiceComponentName());
				System.err.println("ServiceName_"+BaseModelUtil.getFirstPrefix((String)field.getData("id"))+"  "+ gxtToSeamServiceParameterForAdd.getServiceComponentName());
				//Window.alert("ServiceName_"+BaseModelUtil.getFirstPrefix((String)field.getData("id"))+"  "+ gxtToSeamServiceParameterForAdd.getServiceComponentName());
				methodArgumentsForSubmit[0].set("MethodNameForAdd_"+BaseModelUtil.getFirstPrefix((String)field.getData("id")), gxtToSeamServiceParameterForAdd.getServiceMethodName());
				System.err.println("MethodNameForAdd_"+BaseModelUtil.getFirstPrefix((String)field.getData("id"))+"  "+ gxtToSeamServiceParameterForAdd.getServiceMethodName());;
				//Window.alert("MethodNameForAdd_"+BaseModelUtil.getFirstPrefix((String)field.getData("id"))+"  "+ gxtToSeamServiceParameterForAdd.getServiceMethodName());;
				methodArgumentsForSubmit[0].set("MethodNameForUpdate_"+BaseModelUtil.getFirstPrefix((String)field.getData("id")), gxtToSeamServiceParameterForUpdate.getServiceMethodName());
				System.err.println("MethodNameForUpdate_"+BaseModelUtil.getFirstPrefix((String)field.getData("id"))+"  "+ gxtToSeamServiceParameterForUpdate.getServiceMethodName());
				//Window.alert("MethodNameForUpdate_"+BaseModelUtil.getFirstPrefix((String)field.getData("id"))+"  "+ gxtToSeamServiceParameterForUpdate.getServiceMethodName());
*/								
				

			}
			//提取Field的其他子类型的控件中的数据，并封装到，作为服务方法中的参数，提交
			else if(field.isDirty()) {
				System.out.println(field.getName()+" is dirty");
//				methodArgumentsForSubmit[0].set(field.getName(), field.getValue());
				id = (String)field.getData("id");
				System.out.println(field.getValue());
				/*BaseModelUtil.encapsulateComponentData(methodArgumentsForSubmit[0], id, field.getValue());
				BaseModelUtil.encapsulateComponentData(methodArgumentsForSubmit[0], 
						BaseModelUtil.getPrefix(id)+".seamComponetName", seamComponetName);*/
				
//				methodArgumentsForSubmit[0].set(, field.getValue());
				BaseModelUtil.encapsulateComponentData(methodArgumentsForSubmit[0], id, field.getValue());
				
				
				GxtToSeamServiceParameter gxtToSeamServiceParameterForAdd = field.getData("gxtToSeamServiceParameterForAdd");
				GxtToSeamServiceParameter gxtToSeamServiceParameterForUpdate = field.getData("gxtToSeamServiceParameterForUpdate");
				if (gxtToSeamServiceParameterForAdd!=null&&gxtToSeamServiceParameterForUpdate!=null) {
					methodArgumentsForSubmit[0].set("ServiceName_"+BaseModelUtil.getFirstPrefix((String)field.getData("id")), gxtToSeamServiceParameterForAdd.getServiceComponentName());
					System.err.println("ServiceName_"+BaseModelUtil.getFirstPrefix((String)field.getData("id"))+"  "+ gxtToSeamServiceParameterForAdd.getServiceComponentName());
				//	Window.alert("ServiceName_"+BaseModelUtil.getFirstPrefix((String)field.getData("id"))+"  "+ gxtToSeamServiceParameterForAdd.getServiceComponentName());
					methodArgumentsForSubmit[0].set("MethodNameForAdd_"+BaseModelUtil.getFirstPrefix((String)field.getData("id")), gxtToSeamServiceParameterForAdd.getServiceMethodName());
					System.err.println("MethodNameForAdd_"+BaseModelUtil.getFirstPrefix((String)field.getData("id"))+"  "+ gxtToSeamServiceParameterForAdd.getServiceMethodName());;
					//Window.alert("MethodNameForAdd_"+BaseModelUtil.getFirstPrefix((String)field.getData("id"))+"  "+ gxtToSeamServiceParameterForAdd.getServiceMethodName());;
					methodArgumentsForSubmit[0].set("MethodNameForUpdate_"+BaseModelUtil.getFirstPrefix((String)field.getData("id")), gxtToSeamServiceParameterForUpdate.getServiceMethodName());
					System.err.println("MethodNameForUpdate_"+BaseModelUtil.getFirstPrefix((String)field.getData("id"))+"  "+ gxtToSeamServiceParameterForUpdate.getServiceMethodName());
					//Window.alert("MethodNameForUpdate_"+BaseModelUtil.getFirstPrefix((String)field.getData("id"))+"  "+ gxtToSeamServiceParameterForUpdate.getServiceMethodName());
				};
				

			}
			
		}
		
		return methodArgumentsForSubmit[0];
		
	}
	
	
	
	/**
	 * 
	 * 
	 * <ul>
	 * <li>方法含义：用于查找formPanel中的所有Gird</li>
	 * <li>方法作者：花宏宇</li>
	 * <li>编写日期：2012-2-18；时间：下午09:01:58</li>
	 * </ul>
	 * <ul>
	 * <b>修订编号：</b>
	 * <li>修订日期：</li>
	 * <li>修订作者：</li>
	 * <li>修订原因：</li>
	 * <li>修订内容：</li>
	 * </ul>
	 * @param c
	 * @param grids
	 * @return
	 */
	private static void locateGrids(Container<Component> c, List<Grid<?>> grids) {
		if(c!=null){
			 for (Component comp : c.getItems()) {
			      if (comp instanceof Grid) {
			        grids.add((Grid) comp);
			      } else if (comp instanceof Container) {
			    	  locateGrids((Container) comp, grids);
			      }
			   }
		}
		   
	}
	private static FormPanel createDefaultFormPanel() {

		FormPanel formpanel = new FormPanel();
		formpanel.setLayout(new FormLayout());
		formpanel.setHeading("formPanel");
		//formpanel.setSize(300, 200);
		//formpanel.setAutoHeight(true);
		return formpanel;
	}
}
